package com.g2room.myuall.grap.univ;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.g2room.myuall.grap.univ.po.ProvinceScorePo;
import com.g2room.myuall.grap.univ.to.AdminScoreTo;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.qiniu.util.Json;

public class GrapEolUnivScore implements Runnable {
//	// 抓取各省分数
	// 抓取各个大学的各省分数
//	static String pageUrl =  "http://data.api.gkcx.eol.cn/soudaxue/queryProvince.html?messtype=json&url_sign=queryprovince&province3=&year3=&size=10&luqutype3=&luqupici3=&schoolsort=&suiji=&callback=jQuery&_=1470649027111&page="; 
	static String pageUrl = "http://data.api.gkcx.eol.cn/soudaxue/queryProvinceScore.html?messtype=json&url_sign=queryProvinceScore&provinceforschool=&schooltype=&size=50&keyWord=&schoolproperty=&schoolflag=&schoolsort=&province=&fsyear=&fstype=&zhaoshengpici=&suiji=&callback=j&_=1470668220328&page="; 
	static Map<String, Boolean> grapedUrls = getGrapedUrl();
	
	static List<String> errList = new ArrayList<String>();
	static {
		
	}
	
	public static void main(String[] args) {
		GrapEolUnivScore gue = new GrapEolUnivScore();
//			gue.getList("http://data.api.gkcx.eol.cn/soudaxue/queryProvince.html?messtype=json&url_sign=queryprovince&province3=&year3=&size=10&luqutype3=&luqupici3=&schoolsort=&suiji=&callback=jQuery&_=1470649027111&page=122");
//		// 以下为抓取 167 -->10  36 -- 50  383700----384
//		for(int i =0; i<30; i++) {
//			try {
//				Thread.sleep(100);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//			GrapEolUnivScore gue = new GrapEolUnivScore();
//			Thread t = new Thread(gue);
//			t.start();
//		}
		

		for(int i =1; i<3; i++) {
			gue.getList(pageUrl + i);
		}
		
//		for(String url : errList) {
////			Boolean r = grapedUrls.get(url);
////			if(null != r && r) {
////				continue;
////			}
//			System.out.println(url);
//			gue.getList(url);
//		}
	}

	@Override
	public void run() {
		List<String> listUrls = getUrl();
		if(null == listUrls) return;
		for(String url : listUrls) {
			getList(url);
		}
	}
	
	public static AtomicInteger page = new AtomicInteger(1);
	private static synchronized List<String> getUrl() {
		if(page.intValue() > 7674) return null;
		int l = 256;
		List<String> listUrls = new ArrayList<String>();
		for(int i = page.intValue(); i< page.intValue() + l; i++) {
			if(i>7674) break;
			Boolean r = grapedUrls.get(pageUrl+i);
			if(null != r && r) {
				continue;
			}
			listUrls.add(pageUrl+i);
		}
		page.addAndGet(l);
		return listUrls;
	}
	
	/**
	 * 获取分页列表数据
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getList(String url) {
		String body = null;
		try {
			body = new GenericMyuall().getJsonContent(url);
		} catch (Exception e1) {
			e1.printStackTrace();
			System.out.println("err*******="+ url);
			return;
		}
		if(null == body) return;
		try {
			body = body.replaceAll("\\[\\]", "\"0\"");
			AdminScoreTo adminScoreTo = Json.decode(body, AdminScoreTo.class);
			for(ProvinceScorePo as : adminScoreTo.getSchool()) {
				insert2Db(as);
			}
			
			insert2GrapedDb(url);
		} catch (Exception e1) {
			e1.printStackTrace();
			System.out.println("err=== "+ url);
			return;
		}
	}
	
	public void insert2Db(ProvinceScorePo as) {
		try {
			PreparedStatement univPs = DbUtil.getConn().prepareStatement("INSERT INTO `univ_prov_score` ("
					+ "province_name,year,batch_name,old_schoolId,school_name,student_type_name,var,var_score,max,min,num,fencha,score) "
					+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
			/**
			 * `id`, `province_id`, `province_name`, `year`, `batch_id`, `batch_name`, `school_id`, `school_name`, `student_type_name`, `student_type_id`, `var`, `var_score`, `max`, `min`, `num`, `fencha`, `score`, `old_schoolId`
			 */
			univPs.setString(1, as.getLocalprovince());
			univPs.setString(2, as.getYear());
			univPs.setString(3, as.getBatch());
			univPs.setInt(4, as.getSchoolid());
			univPs.setString(5, as.getSchoolname());
			univPs.setString(6, as.getStudenttype());
			univPs.setString(7, as.getVar()); 
			univPs.setString(8, as.getVar_score());
			univPs.setString(9, as.getMax());
			univPs.setString(10, as.getMin());
			univPs.setString(11, as.getNum());
			univPs.setString(12, as.getFencha());
			univPs.setString(13, as.getProvincescore());
			univPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
//	public void insert2ErrDb(String url, int type) {
//		try {
//			PreparedStatement errPs = DbUtil.getConn().prepareStatement("INSERT INTO `univ_err` (`url`, `type`) "
//					+ "values(?,?)");
//			errPs.setString(1, url);
//			errPs.setInt(2, type);
//			errPs.execute();
//		} catch(Exception e) {
//			e.printStackTrace();
//		}
//	}
	
	public void insert2GrapedDb(String url) {
		try {
			PreparedStatement grapPs = DbUtil.getConn().prepareStatement("INSERT INTO `univ_graped_1` (`url`) "
					+ "values(?)");
			grapPs.setString(1, url);
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Map<String, Boolean> getGrapedUrl() {
		Map<String, Boolean> its = new HashMap<String, Boolean>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			String sql = "select url from univ_graped_1";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				its.put(rs.getString("url"), true);
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return its;
	}

}